//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CliquetOption.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Instruments/PercentageStrikePayoff.h>
#include <gen/QL/EuropeanExercise.h>
#include <gen/QL/Payoff.h>
#include <gen/QL/Exercise.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Instruments/OneAssetOption.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CCliquetOption::CCliquetOption (Cephei::QL::Instruments::IPercentageStrikePayoff^ payoff, Cephei::QL::IEuropeanExercise^ maturity, Cephei::IVector<DateTime>^ resetDates, Cephei::QL::IPricingEngine^ QL_Pricer) : COneAssetOption(CCliquetOption::typeid)
{
    CPercentageStrikePayoff^ _Cpayoff;
    CEuropeanExercise^ _Cmaturity;
    CoVector<DateTime>^ _CresetDates;
    try
    {
#ifdef HANDLE
        _phCliquetOption = NULL;
#endif
        _Cpayoff = safe_cast<CPercentageStrikePayoff^> (payoff);
        _Cpayoff->Lock();
        boost::shared_ptr<QuantLib::PercentageStrikePayoff>& _payoff = static_cast<boost::shared_ptr<QuantLib::PercentageStrikePayoff>&> (_Cpayoff->GetShared ()); 
        _Cmaturity = safe_cast<CEuropeanExercise^> (maturity);
        _Cmaturity->Lock();
        boost::shared_ptr<QuantLib::EuropeanExercise>& _maturity = static_cast<boost::shared_ptr<QuantLib::EuropeanExercise>&> (_Cmaturity->GetShared ()); 
        CoVector<DateTime>^ _CresetDates = safe_cast<CoVector<DateTime>^> (resetDates);
        _CresetDates->Lock();
        INativeVector<DateTime>^ _NCIresetDates = _CresetDates->getFeature (NativeFeature::Value);
        CDateTimeVector^ _NCresetDates = safe_cast<CDateTimeVector^>(_NCIresetDates);
        std::vector<QuantLib::Date>& _resetDates = static_cast<std::vector<QuantLib::Date>&> (_NCresetDates->GetReference ());
        _ppCliquetOption = new boost::shared_ptr<QuantLib::CliquetOption> (new QuantLib::CliquetOption ( _payoff,  _maturity,  _resetDates ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppCliquetOption)->setPricingEngine (_QL_Pricer);
        SetOneAssetOption (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCliquetOption));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpayoff != nullptr) _Cpayoff->Unlock();
        if (_Cmaturity != nullptr) _Cmaturity->Unlock();
        if (_CresetDates != nullptr) _CresetDates->Unlock();
    }
}
Cephei::QL::Instruments::CCliquetOption::CCliquetOption (boost::shared_ptr<QuantLib::CliquetOption>& childNative, Object^ owner) : COneAssetOption(CCliquetOption::typeid)
{
#ifdef HANDLE
	_phCliquetOption = NULL;
#endif
	_ppCliquetOption = &childNative;
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCliquetOption));
}
Cephei::QL::Instruments::CCliquetOption::CCliquetOption (QuantLib::CliquetOption& childNative, Object^ owner) : COneAssetOption(CCliquetOption::typeid)
{
#ifdef HANDLE
	_phCliquetOption = NULL;
#endif
	_ppCliquetOption = new boost::shared_ptr<QuantLib::CliquetOption> (&childNative);
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCliquetOption));
    _CliquetOptionOwner = owner;
    _OneAssetOptionOwner = owner;
}

Cephei::QL::Instruments::CCliquetOption::CCliquetOption (CCliquetOption^ copy) : COneAssetOption(CCliquetOption::typeid)
{
#ifdef HANDLE
	_phCliquetOption = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCliquetOption = new boost::shared_ptr<QuantLib::CliquetOption> (copy->GetShared());
        _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCliquetOption));
    }
}
Cephei::QL::Instruments::CCliquetOption::CCliquetOption (System::Type^ t) : COneAssetOption(CCliquetOption::typeid)
{
#ifdef HANDLE
	_phCliquetOption = NULL;
#endif
	if (!t->IsSubclassOf(CCliquetOption::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CCliquetOption::CCliquetOption (QuantLib::Handle<QuantLib::CliquetOption>& childNative, Object^ owner)  : COneAssetOption(CCliquetOption::typeid)
{
	_phCliquetOption = &childNative;
	_ppCliquetOption = &static_cast<boost::shared_ptr<QuantLib::CliquetOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCliquetOption));
    _CliquetOptionOwner = owner;
}
Cephei::QL::Instruments::CCliquetOption::CCliquetOption (QuantLib::Handle<QuantLib::CliquetOption> childNative)  : COneAssetOption(CCliquetOption::typeid)
{
	_phCliquetOption = &childNative;
	_ppCliquetOption = &static_cast<boost::shared_ptr<QuantLib::CliquetOption>>(childNative.currentLink());
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCliquetOption));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CCliquetOption::CCliquetOption (QuantLib::CliquetOption childNative)  : COneAssetOption(CCliquetOption::typeid)
{
#ifdef HANDLE
	_phCliquetOption = NULL;
#endif
	_ppCliquetOption = new boost::shared_ptr<QuantLib::CliquetOption> (new QuantLib::CliquetOption (childNative));
    _ppOneAssetOption = new boost::shared_ptr<QuantLib::OneAssetOption> (boost::dynamic_pointer_cast<QuantLib::OneAssetOption> (*_ppCliquetOption));
}
#endif

Cephei::QL::Instruments::CCliquetOption::~CCliquetOption ()
{
    if (_ppCliquetOption != NULL)
    {
	    delete _ppCliquetOption;
        _ppCliquetOption = NULL;
    }
}
Cephei::QL::Instruments::CCliquetOption::!CCliquetOption ()
{
    if (_ppCliquetOption != NULL)
    {
	    delete _ppCliquetOption;
    }
}
QuantLib::CliquetOption& Cephei::QL::Instruments::CCliquetOption::GetReference ()
{
    if (_ppCliquetOption == NULL) throw gcnew NativeNullException ();
	return **_ppCliquetOption;
}
boost::shared_ptr<QuantLib::CliquetOption>& Cephei::QL::Instruments::CCliquetOption::GetShared ()
{
    if (_ppCliquetOption == NULL) throw gcnew NativeNullException ();
	return *_ppCliquetOption;
}
QuantLib::CliquetOption* Cephei::QL::Instruments::CCliquetOption::GetPointer ()
{
    if (_ppCliquetOption == NULL) throw gcnew NativeNullException ();
	return &**_ppCliquetOption;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CliquetOption>& Cephei::QL::Instruments::CCliquetOption::GetHandle ()
{
	if (_phCliquetOption == NULL)
	{
		_phCliquetOption = new Handle<QuantLib::CliquetOption> (*_ppCliquetOption);
	}
	return *_phCliquetOption;
}
#endif
bool Cephei::QL::Instruments::CCliquetOption::HasNative () 
{
	return (_ppCliquetOption != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Instruments::ICliquetOption^ Cephei::QL::Instruments::CCliquetOption_Factory::Create (Cephei::QL::Instruments::IPercentageStrikePayoff^ payoff, Cephei::QL::IEuropeanExercise^ maturity, Cephei::IVector<DateTime>^ resetDates, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return gcnew CCliquetOption ( payoff,  maturity,  resetDates,  QL_Pricer);
}
